package com.yntx.usercenter.authorization.controller;

import com.yntx.usercenter.authorization.dto.*;
import com.yntx.usercenter.authorization.service.RoleService;
import com.yntx.usercenter.authorization.vo.RoleVO;
import com.yntx.usercenter.common.web.ApiResult;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 *
 * @author wuzhengyu
 * @date 2023/05/08
 */
@RestController
@RequestMapping("/role")
@AllArgsConstructor
public class RoleController {

    private RoleService roleService;

    // 角色列表
    @GetMapping("/list")
    public ApiResult<List<RoleVO>> list(RoleDTO roleDTO) {
        HintManager.clear();
        HintManager instance = HintManager.getInstance();
        instance.setDatabaseShardingValue("yntx");
        instance.setDataSourceName("yntx");
        List<RoleVO> result = roleService.list(roleDTO);
        instance.close();
        return ApiResult.ofSuccess(result);
    }

    // 获取某个用户的所有角色
    @GetMapping("/listByUser")
    public ApiResult<List<RoleVO>> getUserAllRoles(@RequestParam Long userId) {
        HintManager.clear();
        HintManager instance = HintManager.getInstance();
        instance.setDatabaseShardingValue("yntx");
        instance.setDataSourceName("yntx");
        List<RoleVO> result = roleService.listByUser(userId);
        instance.close();
        return ApiResult.ofSuccess(result);
    }

    // 新增角色
    @PostMapping("/add")
    public ApiResult<Long> add(@RequestBody RoleDTO dto) {
        Long id = roleService.add(dto);
        return ApiResult.ofSuccess(id);
    }

    // 删除角色
    @PostMapping("/delete")
    public ApiResult<Boolean> delete(@RequestBody RoleDeleteDTO dto) {
        boolean result = roleService.delete(dto);
        return ApiResult.ofSuccess(result);
    }

    // 更新角色
    @PostMapping("/update")
    public ApiResult<Boolean> update(@RequestBody RoleUpdateDTO dto) {
        boolean result = roleService.update(dto);
        return ApiResult.ofSuccess(result);
    }

    // 分配用户角色
    @PostMapping("/assignRole")
    public ApiResult<Boolean> assignRole(@RequestBody AssignRoleDTO dto) {
        boolean result = roleService.assignRole(dto);
        return ApiResult.ofSuccess(result);
    }

    // 取消分配用户角色
    @PostMapping("/unAssignRole")
    public ApiResult<Boolean> unAssignRole(@RequestBody AssignRoleDTO dto) {
        boolean result = roleService.unAssignRole(dto);
        return ApiResult.ofSuccess(result);
    }

    // 分配角色权限
    @PostMapping("/assignPermission")
    public ApiResult<Boolean> assignPermission(@RequestBody AssignPermissionDTO dto) {
        boolean result = roleService.assignPermission(dto);
        return ApiResult.ofSuccess(result);
    }

    // 取消分配角色权限
    @PostMapping("/unAssignPermission")
    public ApiResult<Boolean> unAssignPermission(@RequestBody AssignPermissionDTO dto) {
        boolean result = roleService.unAssignPermission(dto);
        return ApiResult.ofSuccess(result);
    }

}
